package infoBlock

import (
	"errors"
	"strconv"
	"time"

	"gitee.com/wchpub/dwSvrPub"
)

// 即时运算的数据块
type BlockType string

const (
	//数据块名称
	BlockName string = "info"
)

const (
	Key_Zone      BlockType = "zone"      //时区偏移秒
	Key_GameVer   BlockType = "gameVer"   //游戏版本
	Key_WorldTime BlockType = "worldTime" //世界时间
)

// 全部的key
var AllKeys []BlockType = []BlockType{Key_Zone, Key_GameVer, Key_WorldTime}

type Data struct {
}

// 时区偏移秒
func (m *Data) Zone() int32 {
	utcNow := time.Now().UTC()
	localTime := utcNow.In(dwSvrPub.Location)
	_, offset := localTime.Zone()
	return int32(offset)
}

// 游戏版本号
func (m *Data) GameVer() string {
	return dwSvrPub.GameConfig.ServerConfig.Version
}

// 世界时间
func (m *Data) WorldTime() int64 {
	return dwSvrPub.WorldTime()
}

func (m *Data) GetAll() (map[string]string, error) {
	re := make(map[string]string)

	for _, key := range AllKeys {
		v, err := m.GetString(string(key))
		if err != nil {
			return nil, err
		}
		re[string(key)] = v
	}

	return re, nil
}

func (m *Data) GetString(key string) (string, error) {
	switch key {
	case string(Key_Zone):
		return strconv.Itoa(int(m.Zone())), nil
	case string(Key_GameVer):
		return m.GameVer(), nil
	case string(Key_WorldTime):
		return strconv.FormatInt(m.WorldTime(), 10), nil
	}
	return "", errors.New("key not found")
}

func (m *Data) BlockName() string {
	return BlockName
}

// 获取数据块
func GetBlock() *Data {
	return &Data{}
}
